package com.service.cook.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.cook.CmessageDaoY;
import com.po.MyMessage;
import com.service.cook.CmessageServiceY;

@Service
public class CmessageServiceImpY implements CmessageServiceY {
	@Resource(name = "cmessageDaoImpY")
	private CmessageDaoY cmessageDao;

	@Override
	public int addMessage(MyMessage message) {
		Date data = new Date();
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");

		String id = f.format(data);
		message.setMessage_id(id);

		int i = cmessageDao.addMessage(message);
		return i;
	}

	@Override
	public List<MyMessage> getAllMessageByParent(String message_parentid) {
		List<MyMessage> list = cmessageDao.getAllMessageByParent(message_parentid);
		for (int i = 0; i < list.size(); i++) {
			String[] strings = list.get(i).getMessage_info().split(" ", 2);
			list.get(i).setUser_name(strings[0]);
			list.get(i).setUser_impath(strings[1]);
			
		}
		return list;
	}


	@Override
	public List<MyMessage> getAllMessage() {
		List<MyMessage> list = cmessageDao.getAllMessage();
		for (int i = 0; i < list.size(); i++) {
			String[] strings = list.get(i).getMessage_info().split(" ", 2);
			list.get(i).setUser_name(strings[0]);
			list.get(i).setUser_impath(strings[1]);
		
		}
		return list;
	}

	@Override
	public List<MyMessage> getMessageByID(String userid) {
		List<MyMessage> list = cmessageDao.getMessageByID(userid);
		for (int i = 0; i < list.size(); i++) {
			String[] strings = list.get(i).getMessage_info().split(" ", 2);
			list.get(i).setUser_name(strings[0]);
			list.get(i).setUser_impath(strings[1]);
		}
		return list;
	}

	@Override
	public int getNumberByParent(String message_parentid) {
		List<MyMessage> list = cmessageDao.getAllMessageByParent(message_parentid);
		return list.size();
	}

	@Override
	public int delMessage(String message_id) {
		return cmessageDao.delMessage(message_id);
	}

}
